package URLConnection;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.Certificate;

/**
 * Created by dycong on 2017/4/9.
 */
public class URLConnection {
    public static void main(String[] args) throws Exception {
        URLConnection urlTest = new URLConnection();
//        urlTest.test1();
//        urlTest.test3();
        urlTest.test6();
//        urlTest.test5();
    }

    public void test1() throws Exception {
        URL url = new URL("https://www.baidu.com");
        java.net.URLConnection connection = url.openConnection();
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(connection.getInputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(bufferedInputStream));
        System.out.println(reader.readLine());
        System.out.println(reader.readLine());
        System.out.println(reader.readLine());
    }

    public void test2() throws Exception {
        URL url = new URL("https://www.baidu.com");
        java.net.URLConnection rulConnection = url.openConnection();// 此处的urlConnection对象实际上是根据URL的
        // 请求协议(此处是http)生成的URLConnection类
        // 的子类HttpURLConnection,故此处最好将其转化
        // 为HttpURLConnection类型的对象,以便用到
        // HttpURLConnection更多的API.如下:
        HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection;
    }

    public void test3() throws Exception {
        URL url = new URL("https://www.baidu.com");
        java.net.URLConnection rulConnection = url.openConnection();
        rulConnection.setDoOutput(true);//todo 设为true并不获取connection.getInputStream()，至获取connection.getOutputStream();时为POST

        HttpURLConnection connection=(HttpURLConnection)rulConnection;
//        connection.setRequestMethod("POST");
        connection.setDoInput(true);
        connection.setUseCaches(false);
        // 连接，从上述第2条中url.openConnection()至此的配置必须要在connect之前完成，
        connection.connect();
        connection.getOutputStream();
        System.out.println(connection.getRequestMethod());
    }
    public void test4() throws Exception {
        URL url = new URL("https://www.baidu.com");
        java.net.URLConnection rulConnection = url.openConnection();

        HttpURLConnection connection=(HttpURLConnection)rulConnection;
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setRequestProperty("Content-type", "application/x-java-serialized-object");

        // 连接，从上述第2条中url.openConnection()至此的配置必须要在connect之前完成，
        connection.connect();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(connection.getInputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(bufferedInputStream));
        System.out.println(reader.readLine());
        System.out.println(reader.readLine());
        System.out.println(reader.readLine());
    }
    public void test5(){
        String host = "www.java2s.com";
        String file = "/index.html";

        String[] schemes = { "http", "https", "ftp", "mailto", "telnet", "file", "ldap", "gopher",
                "jdbc", "rmi", "jndi", "jar", "doc", "netdoc", "nfs", "verbatim", "finger", "daytime",
                "systemresource" };

        for (int i = 0; i < schemes.length; i++) {
            try {
                URL u = new URL(schemes[i], host, file);
                System.out.println(schemes[i] + " is supported\r\n");
            } catch (Exception ex) {
                System.out.println(schemes[i] + " is not supported\r\n");
            }
        }
    }
    private void test6(){

        String https_url = "https://www.baidu.com/";
        URL url;
        try {

            url = new URL(https_url);
            HttpsURLConnection con = (HttpsURLConnection)url.openConnection();

            //dumpl all cert info
            print_https_cert(con);

            //dump all the content
            print_content(con);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void print_https_cert(HttpsURLConnection con){

        if(con!=null){

            try {

                System.out.println("Response Code : " + con.getResponseCode());
                System.out.println("Cipher Suite : " + con.getCipherSuite());
                System.out.println("\n");

                Certificate[] certs = con.getServerCertificates();
                for(Certificate cert : certs){
                    System.out.println("Cert Type : " + cert.getType());
                    System.out.println("Cert Hash Code : " + cert.hashCode());
                    System.out.println("Cert Public Key Algorithm : "
                            + cert.getPublicKey().getAlgorithm());
                    System.out.println("Cert Public Key Format : "
                            + cert.getPublicKey().getFormat());
                    System.out.println("\n");
                }

            } catch (SSLPeerUnverifiedException e) {
                e.printStackTrace();
            } catch (IOException e){
                e.printStackTrace();
            }

        }

    }

    private void print_content(HttpsURLConnection con){
        if(con!=null){

            try {

                System.out.println("****** Content of the URL ********");
                BufferedReader br =
                        new BufferedReader(
                                new InputStreamReader(con.getInputStream()));

                String input;

                while ((input = br.readLine()) != null){
                    System.out.println(input);
                }
                br.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
public void test7(){

}
}
